import request from '@/utils/request';
import type { Ref } from 'vue';

export interface PurchasePlanDetail {
  purchasePlanDetailId: number;
  planId: number;
  deviceName: string | null;
  deviceModel: string | null;
  quantity: number;
  supplierName: string | null;
  category:string | null;
}

export interface PurchasePlanDetailListResponse {
  current: number;
  total: number;
  pages: number;
  size: number;
  records: PurchasePlanDetail[];
}

export interface FormItemConfig {
  type: 'input' | 'datetime' | 'select'; 
  label: string;
  prop: keyof PurchasePlanDetailModel;
  placeholder?: string;
  inputType?: HTMLInputElement['type'];
  options?: Ref<Array<{label: string, value: any}>>;
  disabled: boolean | Ref<boolean>,
}

export interface PurchasePlanDetailModel {
  purchasePlanDetailId: number | null;
  planId: number;
  deviceName: string | null;
  deviceModel: string | null;
  quantity: number;
  supplierName: string | null;
  category:string | null;
}

export const purchasePlanDetailApi = {
  addPurchasePlanDetail: (data: PurchasePlanDetailModel) => {
    return request<String>({
      method: 'POST',
      url: `/procedure/purchasePlanDetails`,
      data: data
    });
  },

  // 调整 listPurchasePlanDetails 方法以接受一个 params 对象
  listPurchasePlanDetails: (params: any) => {
    return request<PurchasePlanDetailListResponse>({
      method: 'GET',
      url: `/procedure/purchasePlanDetails`,
      params: params // 直接传递 params 对象
    });
  },

  deletePurchasePlanDetail: (detailId: number) => {
    return request<String>({
      method: 'DELETE',
      url: `/procedure/purchasePlanDetails/${detailId}`,
    });
  },

  listDeviceNames: () => {
    return request<string[]>({
      method: 'GET',
      url: `/procedure/devices/names`,
    });
  },

  listDeviceModelsByDeviceName: (deviceName: string) => {
    return request<string[]>({
      method: 'GET',
      url: `/procedure/devices/${deviceName}/models`,
    });
  },

  listSupplierNames: (deviceName: string, model: string) => {
    return request<string[]>({
      method: 'GET',
      url: `/procedure/devices/${deviceName}/models/${model}/suppliers`,
    });
  },


  getCategoryByNameAndModel: (deviceName: string, model: string) => {
    return request<string>({
      method: 'GET',
      url: `/procedure/devices/${deviceName}/models/${model}/category`,
    });
  },
};
